﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using Wulong.Phone.Dianping.Models;
using Wulong.Phone.Dianping.Services;

namespace Wulong.Phone.Dianping.ViewModels
{
    public class ChannelDialogViewModel:DianpingViewModelBase
    {
        public ChannelDialogViewModel()
        {
            _channelList = new ObservableCollection<Channel>();
        }
        
        private Channel _selectedChannel;
        public Channel SelectedChannel
        {
            get { return _selectedChannel; }
            set
            {
                _selectedChannel = value;
                NotifyOfPropertyChange(() => SelectedChannel);
            }
        }
        private ObservableCollection<Channel> _channelList;
        public IEnumerable<Channel> ChannelList
        {
            get { return _channelList; }
        }
        public void LoadChannels()
        {
            if(_channelList.Count>0) return;
            DianpingService
                .ObservableChannel(CityCode)
                .ObserveOnDispatcher()
                .Subscribe(item => _channelList.Add(item));
        }

        public int ChannelId { get; set; }
        public void SwitchChannel()
        {
            if(SelectedChannel==null) return;
            ChannelId = (int)SelectedChannel.Id;
            ChannelName = SelectedChannel.Name;
            TryClose();
        }

        public string ChannelName { get; set; }

        protected override void OnActivate()
        {
            base.OnActivate();
            LoadChannels();
        }
    }
}
